/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.ConfigEntity;
import cn.eclass.entity.Entity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class ConfigDao extends CommonDao {

    private static final String TABLE_NAME = "config";

    public ConfigDao() {
        super(ConfigDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        ConfigEntity entity = new ConfigEntity();
        try {
            entity.setId(resultSet.getInt("ID"));
            entity.setName(resultSet.getString("NAME"));
            entity.setDescription(resultSet.getString("DESCRIPTION"));
            entity.setCategory(resultSet.getString("CATEGORY"));
            entity.setValue(resultSet.getString("VALUE"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return entity;
    }

    public int add(ConfigEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + "(`NAME`,`DESCRIPTION`,`CATEGORY`,`VALUE`) "
                + "values ("
                + "'" + entity.getName() + "',"
                + "'" + entity.getDescription() + "',"
                + "'" + entity.getCategory() + "',"
                + "'" + entity.getValue() + "'"
                + ")";
        return super.insert(sql);
    }

    public boolean update(ConfigEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`NAME` = '" + entity.getName() + "',"
                + "`DESCRIPTION` = '" + entity.getDescription() + "',"
                + "`CATEGORY` = '" + entity.getCategory() + "',"
                + "`VALUE` = '" + entity.getValue() + "'"
                + " where id = " + entity.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public ConfigEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<ConfigEntity> entitys = (List<ConfigEntity>) super.select(sql);
        return entitys.get(0);
    }

    public List<ConfigEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<ConfigEntity>) super.select(sql);
    }
}
